EEL_MODS	+= kernel

$(eval $(call deftargets_git,kernel,KL))
$(eval $(call deftargets_download,kernel,KL,$$(KL_PKG),v$$(KL_VERSION)))

ifeq ($(BR_KL_ENABLE),1)
$(KL_VMLINUX) $(KL_IMAGE) $(DTB_IMAGE) $(O_KL_DIR)/System.map:
	$(EELQ5)$(MAKE_THIS) buildroot
else
PHONY-kernel += kl-%
kl-%: $(BUILD_DEPS)
	$(EELQ2)$(MAKE) $(JOB_OPT) \
		-C $(S_KL_DIR) \
		ARCH=$(KL_ARCH) \
		CROSS_COMPILE=$(TC_PREFIX) \
		O=$(O_KL_DIR) \
		$(patsubst kl-%,%,$@)

kernel: $(BUILD_DEPS)
kernel: kernel-prepare
kernel: kernel-defconfig
	$(EELQ2)$(MAKE) $(JOB_OPT) \
		-C $(S_KL_DIR) \
		ARCH=$(KL_ARCH) \
		CROSS_COMPILE=$(TC_PREFIX) \
		O=$(O_KL_DIR) \
		LOADADDR=$(KL_LOADADDR) \
		$(KL_TARGETS)

$(KL_VMLINUX_GDB_PY):
	$(EELQ5)$(MAKE_THIS) kl-scripts_gdb

PHONY-kernel += kernel-defconfig kernel-menuconfig kernel-savedefconfig
kernel-defconfig: kernel-prepare kl-$(KL_CONFIG)
kernel-menuconfig: kl-menuconfig

kernel-savedefconfig: kl-savedefconfig
ifneq ($(KL_SAVECONF),defconfig)
	$(EELQ5)mkdir -p $(EEL_CHG_DIR)/$(KL_VER_DIR)/arch/$(KL_ARCH)/configs
	$(EELQ)cp $(O_KL_DIR)/defconfig \
		$(S_KL_DIR)/arch/$(KL_ARCH)/configs/$(KL_SAVECONF)
	$(EELQ)cp $(O_KL_DIR)/defconfig \
		$(EEL_CHG_DIR)/$(KL_VER_DIR)/arch/$(KL_ARCH)/configs/$(KL_SAVECONF)
else
	@echo "KL_SAVECONF is defconfig !!!"
endif

PHONY-kernel += kernel-modules kernel-modules_install
kernel-modules: kl-modules

kernel-modules_install: $(BUILD_DEPS) $(SHARE_DIR_ARCH)
	$(EELQ2)$(MAKE) $(JOB_OPT) \
		-C $(S_KL_DIR) \
		ARCH=$(KL_ARCH) \
		CROSS_COMPILE=$(TC_PREFIX) \
		O=$(O_KL_DIR) \
		INSTALL_MOD_PATH=$(SHARE_DIR_ARCH)/$(KL_VER_DIR)/$(KL_CONFIG)/kmods \
		modules_install

$(O_KL_DIR)/.config:
	$(EELQ5)$(MAKE_THIS) kernel-defconfig

$(KL_VMLINUX) $(KL_IMAGE) $(DTB_IMAGE) $(O_KL_DIR)/System.map:
	$(EELQ5)$(MAKE_THIS) kernel

PHONY-kernel += kernel-dis kernel-dis-force
kernel-dis:
	$(EELQ5)$(MAKE_THIS) $(O_KL_DIR)/vmlinux.dis

$(O_KL_DIR)/vmlinux.dis: $(O_KL_DIR)/vmlinux
	$(EELQ5)$(MAKE_THIS) kernel-dis-force

kernel-dis-force: $(BUILD_DEPS) $(KL_IMAGE)
	$(EELQ2)$(TC_PREFIX)objdump $(OBJDUMP_FLAGS) \
		$(O_KL_DIR)/vmlinux \
		> $(O_KL_DIR)/vmlinux.dis
	@echo "Disassemble: $(O_KL_DIR)/vmlinux.dis"

$(BOOTCONFIG):
	$(EELQ5)$(MAKE) -C $(S_KL_DIR)/tools/bootconfig

PHONY-kernel += initrd-addbconf initrd-delbconf initrd-listbconf
initrd-addbconf: $(BOOTCONFIG) $(INITRD_FILE) $(BCONF_FILE)
	$(EELQ)$(BOOTCONFIG) -a $(BCONF_FILE) \
		$(INITRD_FILE)

initrd-delbconf: $(BOOTCONFIG) $(INITRD_FILE)
	$(EELQ)$(BOOTCONFIG) -d \
		$(INITRD_FILE)

initrd-listbconf: $(BOOTCONFIG) $(INITRD_FILE)
	$(EELQ)$(BOOTCONFIG) -l \
		$(INITRD_FILE)
endif
